package edu.xlh.travel.controller.admin;

import edu.xlh.travel.common.OnlineUserInfoInRedisConstant;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.service.AdminService;
import edu.xlh.travel.redis.utils.JedisManager;
import edu.xlh.travel.syslog.annontion.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private JedisManager jedisManager;

    /*
    * login
    * */
    @Log("管理员登录")
    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody Map<String, String> map ,String verKey , HttpSession session , HttpServletResponse response) {
        return adminService.login(map, verKey ,session , response);
    }

    /*
    * to index page
    *  */
    @Log("后台首页跳转")
    @RequestMapping("/index")
    public String toIndex() {
        return "admin/index";
    }

    /*
    * loginOut
    * */
    @Log("注销")
    @RequestMapping("/logout")
    public void loginOut(HttpServletRequest request , HttpServletResponse response , HttpSession session) throws IOException {
        User admin1 = (User) session.getAttribute("admin");
        String userId = admin1.getId();
        jedisManager.del(OnlineUserInfoInRedisConstant.OnlineUserInfo_RD_RESOURCES+"::"+userId);
        Enumeration<String> attributeNames = request.getSession().getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String key = attributeNames.nextElement().toString();
            User admin = (User) request.getSession().getAttribute("admin");
            request.getSession().removeAttribute(key);
        }
        List<Cookie> cookieList = new ArrayList<>();
        Cookie cookie = new Cookie("JSESSIONID" , "");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        for (Cookie cookie1 : cookieList) {
            response.addCookie(cookie1);
        }
        response.sendRedirect(request.getContextPath() + "/dist/adminLogin");
    }

    /*
    * echars
    * */
    @RequestMapping(value = "/echars")
    public String analysis(){
        return "admin/welcome-1";
    }

    /**
     * to userList page
     */
    @Log("跳转用户列表页面")
    @RequestMapping(value = "/userList")
    public String userList(){
        return "admin/usermanage/userList";
    }

    /*
    * to exception page
    * */
    @Log("异常处理页")
    @RequestMapping("/toExceptionPage")
    public String toExceptionPage () {
        return "layuimini-v2.0.6.1-iframe/page/404";
    }

}
